﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Service;
using MODEL;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using System.ComponentModel;
using Common;
using Microsoft.Practices.ServiceLocation;
using GalaSoft.MvvmLight.Views;

namespace ViewModel
{ 
    public class LogOnViewModel : ViewModelBase
    {
        private IUserService m_UserService;
        
        public LogOnViewModel(IUserService userService)
        {
            this.m_UserService = userService;
            this.LogOnCommand = new RelayCommand(LogOn);
        }

        private string m_AccountNumber;
        public string AccountNumber
        {
            get { return m_AccountNumber; }
            set { m_AccountNumber = value; base.RaisePropertyChanged(() => AccountNumber);  }
        }

        private string m_Password;
        public string Password
        {
            get { return m_Password; }
            set { m_Password = value; base.RaisePropertyChanged(() => Password); }
        }

        public ICommand LogOnCommand { get; set; }
        public void LogOn()
        {
            if (string.IsNullOrEmpty(AccountNumber))
            {
                 ServiceLocator.Current.GetInstance<IDialogService>().ShowError("账号不能为空", "错误", "确定", null);
                return;
            }
            if (string.IsNullOrEmpty(Password))
            {
                ServiceLocator.Current.GetInstance<IDialogService>().ShowError("密码不能为空", "错误", "确定", null);
                return;
            }

            try
            {
                if (this.m_UserService.LogOn(this.AccountNumber, this.Password))
                {
                    Messenger.Default.Send<object>(null, "LogOn");
                }
                else
                {
                    Messenger.Default.Send<object>(null, "LogOn");
                }
            }
            catch (Exception ex)
            {
                ServiceLocator.Current.GetInstance<IDialogService>().ShowError(ex.Message, "错误", "确定", null);
            }
        }
    }
}
